package com.gilight.base.service.impl;

import com.gilight.base.dao.SysUserAccessLogMapper;
import com.gilight.base.dto.SysUserAccessLogDto;
import com.gilight.base.model.SysUserAccessLog;
import com.gilight.base.service.ISysUserAccessLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户日志记录实现层
 *
 * @author wuzz
 * @date 14:21 2019/9/16
 */
@Service
public class SysUserAccessLogServiceImpl implements ISysUserAccessLogService {
    @Autowired
    SysUserAccessLogMapper sysUserAccessLogMapper;


    @Override
    public void addSysUserAccessLog(SysUserAccessLog sysUserAccessLog) {
        sysUserAccessLogMapper.insertSelective(sysUserAccessLog);
    }

    @Override
    public PageInfo findList(SysUserAccessLogDto sysUserAccessLogDto){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(sysUserAccessLogDto.getPageNum(), sysUserAccessLogDto.getPageSize());
        List<SysUserAccessLog> list = sysUserAccessLogMapper.findList(map);
        if(list != null){
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH24:mm:ss");
            for(SysUserAccessLog accessLog:list){
                if(accessLog.getType() == 1){
                    accessLog.setTypeName("登录");
                }else{
                    accessLog.setTypeName("退出");
                }
                if(accessLog.getCreateTime() != null){
                    String createTimeStr = sf.format(accessLog.getCreateTime());
                    accessLog.setCreateTimeStr(createTimeStr);
                }
            }
        }
        return new PageInfo(list);
    }
}
